var area_picker = require("./../../components/area-picker/area-picker.js");
Page({
  data: {
    sub_btn: true,
    xyshow: true,
    check: false,
    imageList: [],
    shoose: false,
    haibao: 1,
    haibao_img: '',
    shop_is_tpl: '',
    form_id: ''
  },
  onLoad: function(options) {
    getApp().page.onLoad(this, options);
    var that = this;
    that.getDistrictData(function(t) {
      area_picker.init({
        page: that,
        data: t
      });
    })
    getApp().request({
      url: getApp().api.commander.tuanzhang_type,
      method: 'POST',
      data: {},
      success: function(e) {
        if (options.act == 1) {
          that.setData({
            code: 0,
            data: '',
            info: '',
            url: '',
            shop_set: e.shop_set,
            // shop_is_tpl: e.shop_is_tpl
          })
        } else {
          that.setData({
            code: e.code,
            data: e.data,
            info: e.info,
            url: e.url,
            shop_set: e.shop_set,
            // shop_is_tpl: e.shop_is_tpl
          })
        }
      }
    })
  },
  getDistrictData: function(t) {
    var i = getApp().core.getStorageSync(getApp().const.DISTRICT);
    if (!i) return getApp().core.showLoading({
      title: "正在加载",
      mask: !0
    }), void getApp().request({
      url: getApp().api.default.district,
      success: function(e) {
        getApp().core.hideLoading(), 0 == e.code && (i = e.data, getApp().core.setStorageSync(getApp().const.DISTRICT, i),
          t(i));
      }
    });
    t(i);
  },
  tiaozhuan: function() {
    var that = this;
    wx.redirectTo({
      url: this.data.url,
      fail: function() {
        wx.navigateTo({ //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
          url: that.data.url
        })
      }
    })
  },

  onReady: function() {

  },

  onShow: function() {

  },

  onHide: function() {

  },
  ressadd: function() {
    var that = this;
    getApp().core.chooseLocation({
      success: function(res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
          address: res.address
        });
      },
      fail: function(a) {
        j.getauth({
          content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
          success: function(e) {
            e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
              title: "您取消了授权",
              image: "/images/icon-warning.png"
            }));
          }
        });
      }
    });
  },
  hyxzboxshow: function(e) {
    this.setData({
      xyshow: false
    })
  },
  hyxzboxhide: function(e) {
    this.setData({
      xyshow: true,
      check: true,
      sub_btn: false
    })
  },
  image_show: function() {
    this.setData({
      haibao: 0
    })
  },
  read: function() {
    var check = this.data.check;
    if (check) {
      this.setData({
        check: false,
        sub_btn: true
      })
    } else {
      this.setData({
        check: true,
        sub_btn: false
      })
    }
  },
  chooseImage: function() {
    var that = this
    getApp().core.chooseImage({
      sourceType: ['album', 'camera'],
      count: 1,
      success: function(res) {
        that.setData({
          imageList: res.tempFilePaths,
          shoose: true
        })
      }
    })
  },
  formSubmitMessage: function(e) {
    var that = this;
    if (typeof wx.requestSubscribeMessage === "function") {
      var shop_is_tpl = that.data.shop_is_tpl;
      return wx.requestSubscribeMessage({
        tmplIds: [shop_is_tpl],
        success: function(res) {
          if (shop_is_tpl != '' && res[shop_is_tpl] == "accept") {
            that.data.form_id = shop_is_tpl
          }
        },
        fail: function(res) {

        },
        complete: function(res) {
          that.read(e);
        }
      })
    } else {
      that.read(e);
    }
  },
  formSubmit: function(e) {
    var that = this;
    let {
      address,
      latitude,
      locationAddress,
      longitude,
      mobile,
      name
    } = e.detail.value;
    if (!that.data.imageList[0]) {
      getApp().core.showModal({
        title: '提示',
        content: '请上传头像',
        showCancel: false,
      })
      return false;
    }
    if (!that.data.district) {
      getApp().core.showModal({
        title: '提示',
        content: '请选择地区',
        showCancel: false,
      })
      return false;
    }
    getApp().core.showLoading({
      title: '提交中',
    });

    var upload_task = getApp().core.uploadFile({
      url: getApp().api.default.upload_image,
      filePath: that.data.imageList[0],
      name: "image",
      formData: {
        address: address + locationAddress,
        latitude: latitude,
        longitude: longitude,
        mobile: mobile,
        name: name,
      },
      success: function(res) {
        var data_obj = JSON.parse(res.data);
        if (data_obj.code == 0) {
          var pic_img = data_obj.data.url;
          getApp().request({
            url: getApp().api.commander.tuanzhang_save,
            method: 'POST',
            data: {
              address: address + locationAddress,
              latitude: latitude,
              longitude: longitude,
              mobile: mobile,
              name: name,
              pic_url: pic_img,
              cover_url: pic_img,
              formId: that.data.form_id,
              province: that.data.district.province.id,
              city: that.data.district.city.id,
              district: that.data.district.district.id
            },
            success: function(e) {
              getApp().core.hideLoading();
              if (e.code > 0) {
                getApp().core.showModal({
                  title: '提示',
                  content: e.info,
                  showCancel: false,
                })
              }
              if (e.code == 0) {
                getApp().core.showModal({
                  title: '提示',
                  content: e.info,
                  showCancel: false,
                  success: function(e) {
                    wx.switchTab({ //保留当前页面，跳转到应用内的某个页面（最多打开5个页面，之后按钮就没有响应的）
                      url: "/pages/index/index"
                    })
                  }
                })
              }

            },
            fail: function() {
              getApp().core.showModal({
                title: '提示',
                content: '提交失败',
                showCancel: false,
              })
            }
          })
        } else {
          getApp().core.showToast({
            title: '图片上传出错',
            icon: 'none'
          })
        }
      }
    })
  },

  onAreaPickerConfirm: function(t) {
    this.setData({
      district: {
        province: {
          id: t[0].id,
          name: t[0].name
        },
        city: {
          id: t[1].id,
          name: t[1].name
        },
        district: {
          id: t[2].id,
          name: t[2].name
        }
      }
    });
  },
})